﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Threading;
using System.Windows;

namespace Yugi.Client.WebSerive
{
    public class WebServiceCommunication
    {
        Yugi.Client.YugiWebService.AuthHeader auth = null;

        public void SetAuth(string username,string pass)
        {
            auth = new Yugi.Client.YugiWebService.AuthHeader();
            auth.Username = username;
            auth.Password = pass;
        }
        public PlayerInfo GetAllCard(int player_id)
        {
            if (auth == null)
                return null;
            YugiWebService.GetInfoSoapClient client = new Yugi.Client.YugiWebService.GetInfoSoapClient();
            string res = client.GetCardsOfPlayer(auth, player_id);
            return PlayerInfo.Deserialize(res);
        }
        public List<TableInfo> UpdateListTable(int RoomID)
        {
            if (auth == null)
                return null;
            YugiWebService.GetInfoSoapClient client = new Yugi.Client.YugiWebService.GetInfoSoapClient();
            string res= client.GetAllTable(auth, RoomID);
            return TableInfo.DeSerialization(res);
        }

        public void UpdateListTableAsync(DispatcherObject obj,Action<object> resCallback, int RoomID)
        {
            if (auth == null)
                return;
            else
            {
                Action<object> tmp = (o) =>
                {
                    YugiWebService.GetInfoSoapClient client = new Yugi.Client.YugiWebService.GetInfoSoapClient();
                t:
                    try
                    {
                        string res = client.GetAllTable(auth, RoomID);
                        Action ink = () =>
                        {
                            resCallback(TableInfo.DeSerialization(res));
                        };
                        obj.Dispatcher.Invoke(ink);
                        client.Close();
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show("Can't Connect to WebService. Auto reconnects after 5 seconds", "Error", MessageBoxButton.OK);
                        Thread.Sleep(5000);
                        goto t;
                    }
                    client.Close();
                };
                ThreadPool.QueueUserWorkItem(new WaitCallback(tmp));
            }
        }
        public void UpdateListRoomAsync(DispatcherObject obj, Action<object> resCallback)
        {
            if (auth == null)
                return;
            Action<object> tmp = (o) =>
            {
                YugiWebService.GetInfoSoapClient client = new Yugi.Client.YugiWebService.GetInfoSoapClient();
            t:
                try
                {
                    string res = client.GetAllRoom(auth);
                    Action ink = () =>
                    {
                        resCallback(RoomInfo.DeSerialization(res));
                    };
                    obj.Dispatcher.Invoke(ink);
                    client.Close();
                }
                catch (Exception ex)
                {
                    //MessageBox.Show("Can't Connect to WebService. Auto reconnects after 5 seconds", "Error", MessageBoxButton.OK);
                    Thread.Sleep(5000);
                    goto t;
                }
                client.Close();
            };
            ThreadPool.QueueUserWorkItem(new WaitCallback(tmp));
        }

        public void UpdateListFriendAsync(DispatcherObject obj, Action<object> resCallback, int UserID)
        {
            if (auth == null)
                return;
            Action<object> tmp = (o) =>
            {
                YugiWebService.GetInfoSoapClient client = new Yugi.Client.YugiWebService.GetInfoSoapClient();
            t:
                try
                {
                    string res = client.GetAllFriend(auth, UserID);//test
                    Action ink = () =>
                    {
                        resCallback(PlayerInfo.DeSerialization(res));
                    };
                    obj.Dispatcher.Invoke(ink);
                    client.Close();
                }
                catch (Exception ex)
                {
                    //MessageBox.Show("Can't Connect to WebService. Auto reconnects after 5 seconds", "Error", MessageBoxButton.OK);
                    Thread.Sleep(5000);
                    goto t;
                }
                client.Close();
            };
            ThreadPool.QueueUserWorkItem(new WaitCallback(tmp));
        }
        public void UpdateListUserAsync(DispatcherObject obj, Action<object> resCallback, int RoomID)
        {
            if (auth == null)
                return;
            Action<object> tmp = (o) =>
            {
                YugiWebService.GetInfoSoapClient client = new Yugi.Client.YugiWebService.GetInfoSoapClient();
            t:
                try
                {
                    string res = client.GetAllUser(auth, RoomID);//test
                    Action ink = () =>
                    {
                        resCallback(PlayerInfo.DeSerialization(res));
                    };
                    obj.Dispatcher.Invoke(ink);
                    client.Close();
                }
                catch (Exception ex)
                {
                    //MessageBox.Show("Can't Connect to WebService. Auto reconnects after 5 seconds", "Error", MessageBoxButton.OK);
                    Thread.Sleep(5000);
                    goto t;
                }
                finally
                {
                    client.Close();
                }
            };
            ThreadPool.QueueUserWorkItem(new WaitCallback(tmp));
        }
    }
}
